package com.bfsuol.common.task;

import com.bfsuol.bfsuolframework.core.utils.DateTimeUtils;
import com.bfsuol.common.Constants;
import com.bfsuol.yihang.cars.api.CommandVehicleService;
import com.bfsuol.yihang.cars.api.QueryVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class VehicleInsureTask {

    @Autowired
    private QueryVehicleService queryVehicleService;
    @Autowired
    private CommandVehicleService commandVehicleService;

    @Scheduled(cron = "0 10 0 * * ?")
    public void insureTask() {
        queryVehicleService.findAll().forEach(vehicleDTO -> {
            if (vehicleDTO.getTouBaoEndTime() !=null && DateTimeUtils.getEndDateTime(vehicleDTO.getTouBaoEndTime()).getTime() > System.currentTimeMillis()) {
                Long days = DateTimeUtils.getDaysOfDate(DateTimeUtils.getEndDateTime(vehicleDTO.getTouBaoEndTime()),DateTimeUtils.getCurrentDateTime());
                vehicleDTO.setTouBaoRemainingDays(days.intValue());
                commandVehicleService.updateById(vehicleDTO);
            }else{
                vehicleDTO.setVehicleIsTouBao(Constants.GLOBAL_YESNO_NO);
                commandVehicleService.updateById(vehicleDTO);
            }
        });
        System.out.println("hello world");
    }
}
